package com.atguigu.gulimall.search.controller;

import com.atguigu.common.vo.SearchReq;
import com.atguigu.gulimall.search.service.GulimallSearchService;
import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import com.atguigu.gulimall.search.vo.SearchResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author LiDeJie
 * @Create 2024/1/12 20:39
 * @Version 1.0
 */
@Controller
public class SearchController {
    
    @Autowired
    MallSearchService mallSearchService;
    
    @Autowired
    GulimallSearchService searchService;
    
//    @GetMapping("/list.html")
    public String index(SearchReq searchReq, Model model, HttpServletRequest request) {
        searchReq.set_queryString(request.getQueryString());
        com.atguigu.common.vo.SearchResp searchResp = searchService.search(searchReq);
        model.addAttribute("result", searchResp);
        return "list";
    }
    
    /**
     * 自动将页面提交过来的所有请求查询参数封装成指定的对象
     */
    @GetMapping("/list.html")
    public String listPage(SearchParam searchParam, Model model, HttpServletRequest request) {
        searchParam.set_queryString(request.getQueryString());
        SearchResp result = mallSearchService.search(searchParam);
        model.addAttribute("result", result);
        return "list";
    }
    
}
